package com.atguigu.yygh.cmn.service.impl;


import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.cmn.bean.Student;
import com.atguigu.yygh.cmn.excel.StudentListener;
import com.atguigu.yygh.cmn.listener.DictEvoListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组织架构表 服务实现类

 springcache: spring框架 redis、memcache
   1.redis依赖
   2.application.yml

  3.配置类：@EnableCaching 中提供一个CacheManager RedisTemplate对象

   4.@Cacheable


 redis:
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Cacheable(value = "getChildListByPid", key = "'selectIndexList'+#pid")
    @Override
    public List<Dict> getChildListByPid(Long pid) {
        System.out.println(".......");
        QueryWrapper<Dict> queryWrapper=new  QueryWrapper<Dict>();
        queryWrapper.eq("parent_id",pid);
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        for (Dict dict : dictList) {
            Long id = dict.getId();
            dict.setHasChildren(isHasChildList(id));
        }
        return dictList;
    }

    @Override
    public void download(HttpServletResponse response) throws Exception {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("数字字典列表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        List<Dict> dictList = baseMapper.selectList(null);
        List<DictEeVo> dictEeVoList=new ArrayList<DictEeVo>();

        for (Dict dict : dictList) {
            DictEeVo dictEeVo=new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }
        EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("模板").doWrite(dictEeVoList);
    }

    @Override
    public void importData(MultipartFile file) throws IOException {
        //EasyExcel.read("C:\\Users\\LH\\Desktop\\hello.xlsx", Student.class,new StudentListener()).sheet(1).doRead();
        EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictEvoListener(baseMapper)).sheet(0).doRead();
    }

    @Override
    public String getNameByValue(String dictCode,String value) {
        if(StringUtils.isEmpty(dictCode)){
            QueryWrapper<Dict> queryWrapper=new QueryWrapper<Dict>();
            queryWrapper.eq("value",value);
            return baseMapper.selectOne(queryWrapper).getName();
        }else {
            QueryWrapper<Dict> queryWrapper=new QueryWrapper<Dict>();
            queryWrapper.eq("dict_code",dictCode);
            Long id = baseMapper.selectOne(queryWrapper).getId();

            QueryWrapper<Dict> queryWrapper1=new QueryWrapper<Dict>();
            queryWrapper1.eq("parent_id",id);
            queryWrapper1.eq("value",value);
            return baseMapper.selectOne(queryWrapper1).getName();
        }

    }

    @Override
    public List<Dict> getChildrenByDictcode(String dictCode) {
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<Dict>();
        queryWrapper.eq("dict_code",dictCode);
        Long id = baseMapper.selectOne(queryWrapper).getId();

        QueryWrapper<Dict> queryWrapper1=new QueryWrapper<Dict>();
        queryWrapper1.eq("parent_id",id);

        return baseMapper.selectList(queryWrapper1);
    }

    private boolean isHasChildList(Long pid) {
        QueryWrapper<Dict> queryWrapper=new  QueryWrapper<Dict>();
        queryWrapper.eq("parent_id",pid);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count >0;
    }
}
